package org.reliablesource.prism.gef.editor.commands;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.reliablesource.prism.gef.editor.model.PointerLocation;
import org.reliablesource.prism.gef.editor.model.ScreenAdapter;



/**
 * A command to add a PointerLocation to a ScreenAdapter.
 * The command can be undone or redone.
 */
public class PointerLocationCreateCommand 
extends Command 
{
	
	/** ScreenAdapter to add to. */
	private final ScreenAdapter screen;
	private PointerLocation pointerLocation;
	private final Rectangle bounds;
	
	
	/**
	 * Create a command that will add a new PointerLocation to a ScreenAdapter.
	 * @param pointerLocation the new PointerLocation that is to be added
	 * @param screen the ScreenAdapter that will hold the new element
	 * @throws IllegalArgumentException if any parameter is null, or the request
	 * 						  does not provide a new PointerLocation instance
	 */
	public PointerLocationCreateCommand(PointerLocation _newPointerLocation, 
			final ScreenAdapter _parent, final Rectangle _bounds) {
		this.screen = _parent;
		if(_newPointerLocation == null)
			_newPointerLocation = new PointerLocation(
					new Point(_bounds.getLocation().x, _bounds.getLocation().y), 
					_bounds.getSize());
		this.pointerLocation = _newPointerLocation;		
		bounds = _bounds;
	}
	
	/**
	 * Can execute if all the necessary information has been provided. 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		
		// TODO:	Need to return false if pointerLocations instances > 2
		return pointerLocation != null && screen != null && bounds != null;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		redo();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		screen.addChild(this.pointerLocation);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		screen.removeChild(this.pointerLocation);
	}
	
}